package com.henu.service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.smart4j.framework.dao.DatabaseHelper;
import org.smart4j.framework.ioc.annotation.Inject;
import org.smart4j.framework.tx.annotation.Service;


@Service
public class CartService {
    @Inject
    private UserLoginService userLoginService;

    public List queryCartByUserID(int userID) {
        return DatabaseHelper.queryMapList(
                        "SELECT P.imageUri,p.name,p.id,p.intro,p.price,p.originalPrice FROM CART T,PRODUCTINFO P WHERE T.USERID=? AND T.PRODUCTID = P.ID AND T.STATUS='1'",
                        userID);
    }

    public List queryCartByUsername(String username) {
        Map userBean = userLoginService.queryUserByUsername(username);
        if (userBean != null) {
            return DatabaseHelper.queryMapList("SELECT * FROM CART T WHERE T.USERID=?", userBean.get("id"));
        }
        return Collections.EMPTY_LIST;
    }

    public boolean addCart(int userID, int productID) {
        DatabaseHelper.update("INSERT INTO CART(PRODUCTID,USERID,STATUS) VALUES(?,?,?)", productID, userID,
                "1");
        return true;
    }


    public boolean delCart(int userID) {
        try{
            DatabaseHelper.update("UPDATE CART T SET T.STATUS='0' WHERE T.USERID = ?", userID);
        }
 catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
